package com.homework;

import java.util.*;

/**
 * @author 于振晗
 * @title: HomeWork03
 * @description: TODO
 * @date 2020年09月03日
 * @path com.homework
 */
public class HomeWork03 {
    public static void main(String[] args) {
        List<Student> list = new ArrayList();
        list.add(new Student(1, "张三", 19, "男"));
        list.add(new Student(2, "李四", 16, "女"));
        list.add(new Student(3, "王五", 13, "男"));
        list.add(new Student(4, "赵六", 17, "女"));

        Map<Integer, Student> mp = new HashMap();
        listTomap(list, mp);
        mp.entrySet().forEach(item -> System.out.println(item));
        List<StudentEntry> studentEntries = mapToList(mp);
        studentEntries.forEach(item-> System.out.println(item));


        //经过排序的
        System.out.println("经过排序的");
        Set<StudentEntry> studentEntries1 = mapToTreeSet(mp);
        studentEntries1.forEach(item-> System.out.println(item));

    }

    public static List<StudentEntry> mapToList(Map<Integer,Student> mp) {
        List<StudentEntry> list = new ArrayList();
        mp.entrySet().forEach(item->{
            StudentEntry studentEntry = new StudentEntry(item.getKey(), item.getValue(), new Random().nextInt(100));
            list.add(studentEntry);
        });
        return list;
    }

    public static Set<StudentEntry> mapToTreeSet(Map<Integer,Student> mp) {
        Set<StudentEntry> set = new TreeSet();
        mp.entrySet().forEach(item->{
            StudentEntry studentEntry = new StudentEntry(item.getKey(), item.getValue(), new Random().nextInt(100));
            set.add(studentEntry);
        });
        return set;
    }


    public static void listTomap(List<Student> list, Map<Integer, Student> mp) {
        list.forEach(item -> mp.put(item.getId(), item));
    }

    private static class StudentEntry implements Comparable<StudentEntry>{
        private int key;
        private Student student;
        private int price;

        public StudentEntry(int key, Student student, int price) {
            this.key = key;
            this.student = student;
            this.price = price;
        }

        public int getPrice() {
            return price;
        }

        @Override
        public String toString() {
            return "StudentEntry{" +
                    "key=" + key +
                    ", student=" + student +
                    ", price=" + price +
                    '}';
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;

            StudentEntry that = (StudentEntry) o;

            if (key != that.key) return false;
            if (price != that.price) return false;
            return student != null ? student.equals(that.student) : that.student == null;
        }

        @Override
        public int hashCode() {
            int result = key;
            result = 31 * result + (student != null ? student.hashCode() : 0);
            result = 31 * result + price;
            return result;
        }

        @Override
        public int compareTo(StudentEntry o) {
            return o.getPrice() - price;
        }
    }
}


class Student {
    private int id;
    private String name;
    private int age;
    private String sex;

    public Student(int id, String name, int age, String sex) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.sex = sex;
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Student student = (Student) o;

        if (id != student.id) return false;
        if (age != student.age) return false;
        if (name != null ? !name.equals(student.name) : student.name != null) return false;
        return sex != null ? sex.equals(student.sex) : student.sex == null;
    }

    @Override
    public int hashCode() {
        int result = id;
        result = 31 * result + (name != null ? name.hashCode() : 0);
        result = 31 * result + age;
        result = 31 * result + (sex != null ? sex.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", sex='" + sex + '\'' +
                '}';
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}



